package adapletplugin.dialogs;

import objects.AdapletConstants;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoadEnoviaDataDialog extends TitleAreaDialog
{

	private String typeFile;
	private String relationshipFile;
	private String interfaceFile;
	private String attributeFile;

	private Text typeFileText;
	private Text relationshipFileText;
	private Text interfaceFileText;
	private Text attributeFileText;

	private boolean isLoggedIn;

	public LoadEnoviaDataDialog(Shell parentShell)
	{
		super(parentShell);

		typeFile = "";
		relationshipFile = "";
		interfaceFile = "";
		attributeFile = "";
	}

	@Override
	public void create()
	{
		super.create();

		setTitle("Load Enovia Data");
		setMessage("Follow the instructions below to upload the required information from Enovia.");
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		GridLayout layout = new GridLayout();
		parent.setLayout(layout);

		Composite composite = new Composite(parent, SWT.NULL);
		layout = new GridLayout(1, true);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		// Start Type

		Label label = new Label(composite, SWT.NULL);
		label.setText("Run the following command to create a file containing all the required type information:");
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		Text text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text.setText("list type * select name attribute dump | output typeInfo.txt;");
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		Composite row = new Composite(composite, SWT.NULL);
		row.setLayout(new GridLayout(3, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		label = new Label(row, SWT.NULL);
		label.setText("Type File:");

		typeFileText = new Text(row, SWT.BORDER | SWT.READ_ONLY);
		typeFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		typeFileText.setText("");
		typeFileText.addModifyListener(createModifyListener());

		Button button = new Button(row, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(createBrowseListener(typeFileText));

		// Start Relationships

		label = new Label(composite, SWT.NULL);
		label.setText("Run the following command to create a file containing all the required relationship information:");
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text.setText("list relationship * select name attribute dump | output relationshipInfo.txt;");
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		row = new Composite(composite, SWT.NULL);
		row.setLayout(new GridLayout(3, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		label = new Label(row, SWT.NULL);
		label.setText("Relationship File:");

		relationshipFileText = new Text(row, SWT.BORDER | SWT.READ_ONLY);
		relationshipFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		relationshipFileText.setText("");
		relationshipFileText.addModifyListener(createModifyListener());

		button = new Button(row, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(createBrowseListener(relationshipFileText));

		// Start Interfaces

		label = new Label(composite, SWT.NULL);
		label.setText("Run the following command to create a file containing all the required interface information:");
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text.setText("list interface * select name attribute dump | output interfaceInfo.txt;");
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		row = new Composite(composite, SWT.NULL);
		row.setLayout(new GridLayout(3, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		label = new Label(row, SWT.NULL);
		label.setText("Interface File:");

		interfaceFileText = new Text(row, SWT.BORDER | SWT.READ_ONLY);
		interfaceFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		interfaceFileText.setText("");
		interfaceFileText.addModifyListener(createModifyListener());

		button = new Button(row, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(createBrowseListener(interfaceFileText));

		// Start Attributes

		label = new Label(composite, SWT.NULL);
		label.setText("Run the following command to create a file containing all the required attribute information:");
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text.setText("list attribute * select name type dump | output attributeInfo.txt;");
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		row = new Composite(composite, SWT.NULL);
		row.setLayout(new GridLayout(3, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		label = new Label(row, SWT.NULL);
		label.setText("Attribute File:");

		attributeFileText = new Text(row, SWT.BORDER | SWT.READ_ONLY);
		attributeFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		attributeFileText.setText("");
		attributeFileText.addModifyListener(createModifyListener());

		button = new Button(row, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(createBrowseListener(attributeFileText));

		return parent;
	}

	private ModifyListener createModifyListener()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (typeFileText.getText() == null || typeFileText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (relationshipFileText.getText() == null || relationshipFileText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (interfaceFileText.getText() == null || interfaceFileText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (attributeFileText.getText() == null || attributeFileText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else
				{
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};
	}

	private SelectionListener createBrowseListener(final Text textField)
	{
		return new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e)
			{
				FileDialog dialog = new FileDialog(getParentShell(), SWT.OPEN);
				dialog.open();

				String path = dialog.getFilterPath();
				String fileName = dialog.getFileName();

				if (path == null || fileName == null)
				{
					System.out.println("returning");
					return;
				}

				textField.setText(path + AdapletConstants.sFileSeparator + fileName);
			}
		};
	}

	public String getTypeFile()
	{
		return typeFile;
	}

	public String getRelationshipFile()
	{
		return relationshipFile;
	}

	public String getInterfaceFile()
	{
		return interfaceFile;
	}

	public String getAttributeFile()
	{
		return attributeFile;
	}

	public boolean isLoggedIn()
	{
		return isLoggedIn;
	}

	@Override
	protected void okPressed()
	{
		typeFile = typeFileText.getText();
		relationshipFile = relationshipFileText.getText();
		interfaceFile = interfaceFileText.getText();
		attributeFile = attributeFileText.getText();

		super.okPressed();
	}
}
